package com.changil.nowonchangil.api;

import java.util.ArrayList;

import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.changil.nowonchangil.controller.ServiceController;
import com.changil.nowonchangil.dto.Article;
import com.changil.nowonchangil.dto.ArticleList;
import com.changil.nowonchangil.dto.InsertedId;

@Path("/articles")
public class ArticleApi {
	private ServiceController controller = new ServiceController();

	public void SetScoreService(ServiceController controller) {
		this.controller = controller;
	}

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public ArticleList getArticles(@QueryParam("boardId") int boardId,
			@QueryParam("fromId") int fromId,
			@QueryParam("count") int count,
			@QueryParam("isDownSide") boolean isDownSide) {
		ArticleList result = new ArticleList();
		ArrayList<Article> list = controller.getArticles(boardId, fromId, count, isDownSide);
		result.size = list.size();
		result.list = list;
		return result;
	}

	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Path("article")
	public InsertedId addArticle(@FormParam("boardId") final int boardId,
			@FormParam("title") final String title,
			@FormParam("content") final String content) {
		InsertedId insertedId = new InsertedId();
		insertedId.id = controller.addArticle(boardId, title, content);
		return insertedId;
	}

	@PUT
	@Produces(MediaType.APPLICATION_JSON)
	@Path("article")
	public String updateArticle(@FormParam("id") final int id,
			@FormParam("title") final String newTitle,
			@FormParam("content") final String newContent) {
		System.out.println("아티클 저장 : "+newTitle);
		int result = controller.updateArticle(id, newTitle, newContent);
		return (-1 < result)?ApiConstants.RESULT_SUCESS:ApiConstants.RESULT_FAILED;
	}

	@DELETE
	@Produces(MediaType.APPLICATION_JSON)
	@Path("article")
	public String removeHistory(@QueryParam("id") final int id) {
		int result = controller.deleteArticle(id);
		return (-1 < result)?ApiConstants.RESULT_SUCESS:ApiConstants.RESULT_FAILED;
	}
}
